package com.novunda.license.core;

import com.novunda.license.sm2.Sm2LoadSigleton;

/**
 * SM2加密
 *
 * @author linfeng
 * @since 2016/10/12.
 */
final class SM2Encoder implements LicenseEncode {

    private final String privateKey;

    public SM2Encoder(String privateKey) {
        this.privateKey = privateKey;
    }

    public String getPrivateKey() {
        return privateKey;
    }

    @Override
    public String encode(LicenseContent content) throws LicenseException {

        byte[] cipherTextDecode = PersistenceService.store2ByteArray(content);
        byte[] deviceInforDecrypt = Sm2LoadSigleton.INSTANCE.special_encrypt(privateKey, cipherTextDecode);
        if (deviceInforDecrypt == null) {
            throw new LicenseException("加密失败");
        }
        return Base58.encode(deviceInforDecrypt);
    }

}
